import xlrd
import time
from flask import Blueprint
from flask import request
from app.models.Student import Student


hello_blueprint = Blueprint('hello', __name__, url_prefix='/hello')


@hello_blueprint.route('', methods=['GET'])
def hello_world():
    return "hello world"


@hello_blueprint.route('/excel', methods=['GET', 'POST'])
def read_excel():
    file = request.files['file']
    f = file.read()  # 文件内容
    data = xlrd.open_workbook(file_contents=f)
    table = data.sheets()[0]
    names = data.sheet_names()  # 返回book中所有工作表的名字
    status = data.sheet_loaded(names[0])  # 检查sheet1是否导入完毕
    nrows = table.nrows  # 获取该sheet中的有效行数
    ncols = table.ncols  # 获取该sheet中的有效列数
    print(nrows)
    for i in range(1, nrows):
        s = table.row_values(i)
        for info in s:
            print(info)
    return 'OK'

@hello_blueprint.route('/db', methods=['GET'])
def get_student():
    id = request.get_json()
    student = Student.get_by_student_id(student_id=id.get('student_id'))
    print(student.id)
    return "ok"
